The present invention relates generally to efficient use of computer memory in carrying out program instructions, and specifically to methods and apparatus for garbage collection, i.e., for automatic reclamation of unused memory.
Programming languages such as Java relieve the programmer of the burden of explicit memory management through the use of automatic garbage collection (GC) techniques that are applied xe2x80x9cbehind the scenes.xe2x80x9d When a data object is created, space for the object is allocated in the heap. Unused data objects, which are no longer reachable by the running program via any path of pointer traversals, are considered xe2x80x9cgarbage.xe2x80x9d GC automatically reclaims computer storage assigned to such objects, in order to free the storage for reuse. This makes programming in garbage-collected languages significantly easier than in C or C++, for example, in which the programmer must include an explicit xe2x80x9cfreexe2x80x9d statement in order to reclaim memory. GC allows many run-time errors to be avoided and naturally supports modular programming.
A variety of different GC techniques are known in the art. In mark-sweep garbage collectors, garbage collection is implemented in two successive stages. In a first stage, an object graph is created, tracing the interrelation of objects starting from specified roots and traversing all connected objects in the heap. Objects that are reachable on this graph are considered live objects. Any other object is considered garbage and can be collected. The live objects are marked in some way so as to distinguish between live objects and garbage. In a second stage, the memory is swept, and all memory space occupied by unmarked objects (garbage) is reclaimed, so that it is free to be reallocated. During the sweep stage, the marked objects are unmarked, in preparation for the next GC cycle.
In xe2x80x9cconcurrentxe2x80x9d GC, the execution of application program threads that may update and change the object graph goes on concurrently with the marking and sweeping operations carried out by a collector thread. For this reason, threads of the running program are referred to as xe2x80x9cmutators,xe2x80x9d since they mutate, or change, the object graph. Although the concurrent approach avoids processor inactivity during GC, the running program may change the object graph even during the very steps of tracing out reachable data objects by the collector. As a result, there is a risk that the collector may miss marking a live object, and the live object will then be reclaimed during the sweep phase of the collector. In order to avoid this possibility, synchronization between the mutator and collector threads is essential.
xe2x80x9cOn-the-flyxe2x80x9d concurrent GC schemes use implicit synchronization between the mutator and collector threads in order to allow the threads to run concurrently without having to stop for synchronization. This type of GC was first described by Dijkstra et al., in xe2x80x9cOn-the-Fly Garbage Collection: An Exercise in Cooperation,xe2x80x9d published in Communications of the ACM 21:11 (1978), pages 966-975, which is incorporated herein by reference. Reachable objects are marked by assigning a different xe2x80x9ccolorxe2x80x9d attribute to each object, with xe2x80x9cwhitexe2x80x9d indicating unmarked objects, and xe2x80x9cblackxe2x80x9d indicating marked objects. At the beginning of a GC cycle, all objects are white. Whenever a mutator uses a white object, it marks the object xe2x80x9cgray.xe2x80x9d When the collector encounters a gray object, it knows that while the object is alive, its direct descendants in the pointer graph may not yet have been marked (i.e., some may still be white). On the other hand, when an object is marked black, all of its direct descendants are necessarily marked as well, either gray or black. During the mark/trace phase, the collector traces the graph of live objects, and in doing so changes the color of all gray objects to black and their descendants to gray, continuing until no untraced gray objects remain. After all of the live objects have been traced, the collector then sweeps: white objects are reclaimed and appended to the list of free memory, while black objects are changed to white in preparation for the next collection cycle.
Dijkstra""s approach, while conceptually valuable, has inherent inefficiencies that have prevented it from being widely implemented in practice. Doligez and associates have attempted to overcome these limitations by adding a fourth color: xe2x80x9cblue.xe2x80x9d During the sweep phase, the collector marks white objects as blue, to distinguish them as free. This approach is described by Doligez and Leroy, in xe2x80x9cA Concurrent Generational Garbage Collector for a Multithreaded Implementation of ML,xe2x80x9d published in Proceedings of the 20th Symposium on Principles of Programming Languages (1993), pages 113-123; and by Doligez and Gonthier, in xe2x80x9cPortable Unobtrusive Garbage Collection for Multi-Processor Systems,xe2x80x9d published in the Conference Record of the Twenty-first Annual ACM Symposium on Principles of Programming Languages (1994), pages 70-83. Both of these publications are incorporated herein by reference.
Marking free memory as blue frees the collector from having to trace the list of free memory, but it obligates the mutators to properly color all new objects that they allocate (i.e., objects they create from free memory). The proper color for allocation depends on the stage of the collection cycle currently being executed by the collector thread. While no GC is taking place and at the start of the collection cycle, the proper color is white. At a transition point for each mutator during the mark/trace phase of the collector (the point at which the collector has marked the mutator""s local stack), the proper allocation color becomes black. During the sweep phase of the collector, the color for allocation can be white, gray or black, depending on the address of the object being created relative to the progress of the collector in sweeping the heap.
The collector uses a handshaking protocol to synchronize the mutators with its state, so that the mutators use the correct coloring. Proper execution of the protocol is critical: if a newly-allocated object is colored white at the wrong time, it will be incorrectly collected. If it is incorrectly colored black, before its immediate descendants have been marked, the descendants may be incorrectly collected. This problem can be alleviated by extending the period during which the mutators color objects gray, since neither gray objects nor their descendants are collected in any given GC cycle. This solution, however, runs counter to the primary goal of GC, which is to free unused memory.
One alternative solution to the problem of synchronizing the mutators with the collector is to switch the meaning of the colors xe2x80x9cblackxe2x80x9d and xe2x80x9cwhitexe2x80x9d from one GC cycle to the next. An approach of this sort is described by Hudak and Keller, in xe2x80x9cGarbage Collection and Task Deletion in Distributed Applicative Processing Systems,xe2x80x9d published in the ACM Symposium on Lisp and Functional Programming (1982), pages 168-178, which is incorporated herein by reference. This approach, however, is designed to work in a specific parallel processing system, in which the role of the program stack is taken over by a xe2x80x9cmarking-treexe2x80x9d of tasks. It does not appear to be of general applicability in on-the-fly GC for use with Java and other common software environments.
Preferred embodiments of the present invention provide a method for on-the-fly GC in which objects are colored before they are created, thus relieving the mutators of a major part of the burden of object coloring. This technique is referred to hereinafter as xe2x80x9cpre-coloring.xe2x80x9d Preferably, the mutators pre-color blocks of memory while preparing them for allocation, as a batch operation, instead of coloring objects singly as they are created as in methods known in the art. Periodically, the collector checks the memory that is prepared for allocation to ensure that it has the proper allocation color. Thus, the coloring overhead is borne primarily by the collector, and the speed and efficiency of memory allocation by the mutators are increased. In some embodiments, the elimination of coloring during allocation can also help to prevent race conditions between allocation of objects by mutators and marking of the objects by the collector, which are of concern in on-the-fly GC methods known in the art.
In some preferred embodiments of the present invention, a color switching scheme is used to change the allocation color from one GC cycle to the next. A preferred color switching scheme is described in U.S. patent application Ser. No. 09/167,546, to Kolodner and Lewis, which is assigned to the assignee of the present patent application and is incorporated herein by reference. In this scheme, the constant colors white and black that are used in conventional on-the-fly GC are replaced by global variables xe2x80x9cwhiteColorxe2x80x9d and xe2x80x9cblackColor,xe2x80x9d whose values are interchanged after every GC cycle. For the purposes of the present invention, another global variable, xe2x80x9callocationColor,xe2x80x9d is defined for assignment to blocks of memory being prepared for allocation by the mutators. Between one collection cycle and the next, the value of allocationColor is equal to the blackColor of the preceding cycle (which is the whiteColor of the next cycle). The collector preferably updates allocationColor only once per allocation cycle, from the whiteColor to the blackColor of that cycle.
Although these preferred embodiments use block-based memory allocation and the color switching scheme of Kolodner and Lewis, the principles of the present invention are also applicable to other methods of memory allocation and concurrent GC, including on-the-fly GC. For example, the techniques of pre-coloring taught by the present invention may be used, mutatis mutandis, with the color switching scheme of Hudak and Keller, or with the conventional, fixed-color scheme of Doligez and associates, as described in the Background of the Invention.
There is therefore provided, in accordance with a preferred embodiment of the present invention, a method for memory management in execution of a program by a computer having a memory, including:
setting an attribute of an unallocated unit of the memory in preparation for allocation of the memory in the unit;
allocating respective portions of the memory in the unit to data objects to be held in a heap created by the program;
responsive to the attribute that is set on the allocated portions of the memory, tracing the data objects in the heap so as to mark the data objects that are reachable at a given stage in the program; and
sweeping the heap so as to free the memory that is allocated to data objects that are not marked as reachable, for reallocation to new data objects.
Preferably, tracing the data objects and sweeping the heap include tracing the data objects and sweeping the heap periodically in garbage collection (GC) cycles, and wherein setting the attribute includes setting the attribute to a variable value that changes in each cycle to a new value. Most preferably, setting the attribute to the variable value includes providing at least two different values of the attribute, and alternating between the values in consecutive GC cycles. Additionally or alternatively, setting the attribute includes choosing the value of the attribute to set at each cycle such that the data objects allocated with the set value of the attribute will be traced and marked as reachable at the next cycle. Preferably, setting the attribute includes changing the variable value to the new value after beginning the step of tracing the data objects but before sweeping the heap.
Preferably, setting the attribute includes, after changing the variable value to the new value for a given one of the cycles, checking free memory remaining in the unallocated unit that was prepared for allocation prior to the given cycle, so as to ensure that the attribute of all of the free memory remaining is set to the new value. Further preferably, checking the free memory includes determining whether any of the memory in the unit was allocated over a predetermined period preceding the given cycle, and making the unit unavailable for allocation if none of the memory was allocated over the predetermined period. Most preferably, checking the free memory includes changing the attribute of the free memory remaining to the new value. Alternatively, setting the attribute includes, after changing the variable value to the new value for a given one of the cycles, rendering free memory remaining the unallocated unit that was prepared for allocation prior to the given cycle unavailable for allocation in the given cycle.
In a preferred embodiment, setting the attribute includes setting the attribute using a collector thread, which further performs the steps of tracing the data objects and sweeping the heap, and allocating the respective portions of the memory includes preparing the unallocated unit of the memory and allocating the data objects using a mutator thread generated by the program, separate from the collector thread. Preferably, the mutator thread is further operative to alter data pointers associated with the data objects that it has allocated and to change the attribute of the data objects upon altering the pointers so that the objects are marked as reachable. Most preferably, the collector thread and mutator thread operate on the memory concurrently, wherein the mutator thread changes the attribute upon altering the pointers to a value that is assigned by the collector thread, and which varies depending on a stage of progress by the collector thread in tracing the data objects and sweeping the heap. Preferably, allocating the data objects includes allocating the objects using the mutator thread without changing the attribute that has been set by the collector thread.
In a preferred embodiment, the unit of memory includes a block in a block-managed memory system, and wherein allocating the respective portions includes allocating multiple data objects within the block.
There is also provided, in accordance with a preferred embodiment of the present invention, computing apparatus, including:
a memory, arranged to store data; and
a processor, coupled to allocate the memory for execution of a program running on the apparatus by setting an attribute of an unallocated unit of the memory in preparation for allocation of the unit, and allocating respective portions of the memory in the unit to data objects to be held in a heap created by the program, such that responsive to the attribute that is set on the allocated portions of the memory, the processor is operative to trace the data objects in the heap so as to mark the data objects that are reachable at a given stage in the program, and to sweep the heap so as to free the memory that is allocated to data objects that are not marked as reachable, for reallocation to new data objects.
There is additionally provided, in accordance with a preferred embodiment of the present invention, a computer program product, including a computer-readable medium in which program instructions are stored, which instructions, when read by a computer having a memory and arranged to carry out execution of a program using the memory, cause the computer to set an attribute of an unallocated unit of the memory in preparation for allocation of the memory in the unit, to allocate respective portions of the memory in the unit to data objects to be held in a heap created by the program, and responsive to the attribute that is set on the allocated portions of the memory, to trace the data objects in the heap so as to mark the data objects that are reachable at a given stage in the program, and to sweep the heap so as to free the memory that is allocated to data objects that are not marked as reachable, for reallocation to new data objects.
The present invention will be more fully understood from the following detailed description of the preferred embodiments thereof, taken together with the drawings in which: